package kr.or.kead.module;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.StringTokenizer;
import java.util.regex.Pattern;

import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

@SuppressWarnings("serial")
public class PhoneCheck extends JPanel {
	private JComboBox<String> comboFirstNum;
	private JLabel lblNewLabel;
	private JTextField jtf_MidNum;
	private JLabel lblNewLabel_1;
	private JTextField jtf_FinalNum;
	public static final String[] MOBILE_GUK = {"010","011","016","017","018","019"};
	public static final String[] TEL_GUK={
		"02", "031", "032", "033",
		"041", "042", "043", "044",
		"051", "052", "053", "054", "055",
		"061", "062", "063", "064"
	};
	private boolean type;
	
	public PhoneCheck(boolean type) {
		this.type = type; // true : 모바일    false : 집전화
		init();
	}

	private void init() {
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		comboFirstNum = new JComboBox<>();
		if(type) {
			comboFirstNum = new JComboBox<>(MOBILE_GUK);
		}else {
			comboFirstNum = new JComboBox<>(TEL_GUK);
		}	
		/*
		if(type) {
			int len = MOBILE_GUK.length;
			for(int i=0;i<len;i++) {
				comboFirstNum.addItem(MOBILE_GUK[i]);				
			}
		}else {
			int len = TEL_GUK.length;
			for(int i=0;i<len;i++) {
				comboFirstNum.addItem(TEL_GUK[i]);
			}
		}
		*/
		add(comboFirstNum);
		
		
		lblNewLabel = new JLabel(" - ");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		add(lblNewLabel);
		
		jtf_MidNum = new JTextField();
		jtf_MidNum.setHorizontalAlignment(SwingConstants.CENTER);
//		jtf_MidNum.addFocusListener(new jtf_MinNumFocusListener());
		jtf_MidNum.addKeyListener(new jtf_MinNumKeyListener());
		add(jtf_MidNum);
		jtf_MidNum.setColumns(4);
		
		lblNewLabel_1 = new JLabel(" - ");
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
		add(lblNewLabel_1);
		
		jtf_FinalNum = new JTextField();
		jtf_FinalNum.setHorizontalAlignment(SwingConstants.CENTER);
//		jtf_FinalNum.addFocusListener(new jtf_FinalNumFocusListener());
		jtf_FinalNum.addKeyListener(new jtf_FinalNumKeyListener());
		add(jtf_FinalNum);
		jtf_FinalNum.setColumns(4);	
	}
	
	public void setPhone(String phoneNum) {
		StringTokenizer st = new StringTokenizer(phoneNum, "-");
		/*
		int phoneFirstNum = 0;
		switch(st.nextToken()) {
		case "010": phoneFirstNum = 0; break;
		case "011": phoneFirstNum = 1; break;
		case "016": phoneFirstNum = 2; break;
		case "017": phoneFirstNum = 3; break;
		case "018": phoneFirstNum = 4; break;
		case "019": phoneFirstNum = 5; break;
		
		case "02": phoneFirstNum = 0; break;
		case "031": phoneFirstNum = 1; break;
		case "032": phoneFirstNum = 2; break;
		case "033": phoneFirstNum = 3; break;
		case "041": phoneFirstNum = 4; break;
		case "042": phoneFirstNum = 5; break;
		case "043": phoneFirstNum = 6; break;
		case "051": phoneFirstNum = 7; break;
		case "052": phoneFirstNum = 8; break;
		case "053": phoneFirstNum = 9; break;
		case "054": phoneFirstNum = 10; break;
		case "055": phoneFirstNum = 11; break;
		case "061": phoneFirstNum = 12; break;
		case "062": phoneFirstNum = 13; break;
		case "063": phoneFirstNum = 14; break;
		case "064": phoneFirstNum = 15; break;				
		}		
		comboFirstNum.setSelectedIndex(phoneFirstNum);
		*/
		comboFirstNum.setSelectedItem(st.nextToken());
		jtf_MidNum.setText(st.nextToken());
		jtf_FinalNum.setText(st.nextToken());
	}
	
	public String getPhone() {
		return comboFirstNum.getSelectedItem()+"-" + jtf_MidNum.getText()+"-" + jtf_FinalNum.getText();
	}
		
	/*
	private class jtf_MinNumFocusListener extends FocusAdapter {
		public void focusLost(FocusEvent e) {
			try {
				Integer.parseInt(jtf_MidNum.getText());
			} catch (NumberFormatException ee) {
				JOptionPane.showMessageDialog(null, "숫자만 입력");
				jtf_MidNum.setSelectionStart(0);
				jtf_MidNum.requestFocus();
			}			
		}		
	}
	
	private class jtf_FinalNumFocusListener extends FocusAdapter {
		public void focusLost(FocusEvent e) {
			try {
				Integer.parseInt(jtf_FinalNum.getText());
			} catch (NumberFormatException ee) {
				JOptionPane.showMessageDialog(null, "숫자만 입력");
				jtf_FinalNum.setSelectionStart(0);
				jtf_FinalNum.requestFocus();
			}
		}		
	}
	*/
	
	private class jtf_MinNumKeyListener extends KeyAdapter {
		public void keyReleased(KeyEvent e) {
//			int len = jtf_MidNum.getText().length();
//			if (len == 4) {
//				jtf_FinalNum.requestFocus();
//			}
			if(Pattern.matches("^[0-9]{1}$", String.valueOf(e.getKeyChar())) == false&&
					e.getKeyChar() != KeyEvent.VK_ENTER) {
				JOptionPane.showMessageDialog(null, "숫자만 입력");
				jtf_MidNum.setText((String) jtf_MidNum.getText().subSequence(0, jtf_MidNum.getText().length()-1));
			}else {
				if(jtf_MidNum.getText().length() >= 4) {
					jtf_FinalNum.requestFocus();					
				}
			}
		}		
	}
	
	private class jtf_FinalNumKeyListener extends KeyAdapter {
		public void keyReleased(KeyEvent e) {
//			int len = jtf_FinalNum.getText().length();
//			if (len > 4) {
//				JOptionPane.showMessageDialog(null, "숫자 4자리만 입력");
//				jtf_FinalNum.requestFocus();
//			}
			if(Pattern.matches("^[0-9]{1}$", String.valueOf(e.getKeyChar())) == false&&
					e.getKeyChar() != KeyEvent.VK_ENTER) {
				JOptionPane.showMessageDialog(null, "숫자만 입력");
				jtf_FinalNum.setText((String) jtf_FinalNum.getText().subSequence(0, jtf_FinalNum.getText().length()-1));
			}else {
				if(jtf_FinalNum.getText().length() >= 4) {
					jtf_FinalNum.setFocusable(false);					
				}
			}
		}		
	}

}
